﻿
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using YTBZ.PUB.Common;
using YTBZ.BUSINESSLOGIC;

namespace YTBZ.Z11_XD
{
    public partial class XDBZMANAGE : BasePage
    {
        XDBLL bll = new XDBLL();

        #region pageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMSGINFO.Text = "";
            pgShow.SetTarget(dgList, new PagerNavigation.BindDataDelegate(pageBind));
            pgShow.SetStyle(strReqPageCount()); if (IsPostBack && pgShow.CurrPgSpit && pgShow.CurrPageIndex > 0) { pageBind(); }
            if (!IsPostBack)
            {
                pageInit();
                pageBind();
            }
        }
        #endregion

        #region pageInit
        protected override void pageInit()
        {
            TD_TITLE.InnerHtml = Request["TITLE"].ToString();

            txtQInTimeB.Text = strDate();
            StringHelper.SetUIStyle(this, btnImport);//给导入控件加进度控制
        }
        #endregion

        #region pageBind
        protected override void pageBindReset() { pgShow.CurrPageIndex = 0; } //分页页面重新查询时要回到第一页
        protected override void pageBind()
        {
            SortedList SDList = new SortedList();
            SDList["@OrderBH"] = txtQOrderBH.Text;
            SDList["@PCNum"] = txtQPCNum.Text;
            SDList["@InTimeB"] = txtQInTimeB.Text;
            SDList["@InTimeE"] = txtQInTimeE.Text;
            SDList["@CURRPAGESIZE"] = strReqPageCount();
            SDList["@CURRPAGE"] = pgShow.CurrPageIndex;
            int RecordCount = 0;
            DataTable dt = bll.GetXDBZXX(SDList,out RecordCount);

            DataView dv = dt.DefaultView;
            if (ViewState["SortExpression"] != null) { dv.Sort = GetSortDirectiont(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString()); }
            dgList.DataSource = dv;
            dgList.DataBind();

            if (dt.Rows.Count > 0)
            {
                pgShow.proDataBind(RecordCount, pgShow.CurrPageIndex + 1);
                txtPOrderBH.Text = dt.Rows[0]["OrderBH"].ToString();
                txtPPCNum.Text = dt.Rows[0]["PCNum"].ToString();
            }
            tabShow.Visible = true;

            foreach (GridViewRow gr in dgList.Rows)
            {
                string ZXCC = dgList.DataKeys[gr.RowIndex]["ZXCC"].ToString();
                if (ZXCC != "")
                {
                    gr.FindControl("lbtnZXPrint").Visible = true;
                }
            }
            
            
        }
        #endregion

        #region pageOpt 
        protected override void dgListDo(string strCommandName, string strCommandArgument)
        {
            #region 导入（EXCEL）
            if (strCommandName == "导入")
            {
                #region 校验输入
                string strMsgNull = "";
                if (txtEOrderBH.Text == "") { strMsgNull += "订单号 "; }
                if (txtEPCNum.Text == "") { strMsgNull += "批次号 "; }
                if (strMsgNull != "") { jsMessage("请输入：" + strMsgNull); return; }
                #endregion

                if (string.IsNullOrEmpty(fileUp.FileName) == true)
                {
                    jsMessage("请先选择导入文件");
                    return;
                }

                DataTable dtEmp = ExcelToDataSet(fileUp);
                if (dtEmp != null && dtEmp.Rows.Count > 0)
                {
                    int iCount = dtEmp.Columns.Count;
                    if (iCount < 12)
                    {
                        jsMessage("Excel表头格式不正确，请检查！");
                        return;
                    }
                    
                    #region 导入处理
                    SortedList SDListDel = new SortedList();
                    SDListDel["@OrderBH"] = txtEOrderBH.Text;
                    SDListDel["@PCNum"] = txtEPCNum.Text;
                    bll.DelZXBZXX(SDListDel);

                    for (int i = 0; i < dtEmp.Rows.Count; i++)
                    {
                        DataRow dr = dtEmp.Rows[i];

                        SortedList SDList = new SortedList();
                        SDList["@OrderBH"] = txtEOrderBH.Text;
                        SDList["@PCNum"] = txtEPCNum.Text;
                        SDList["@CX"] = txtECX.Text;
                        SDList["@PCS"] = txtEPCS.Text;
                        SDList["@TXBH"] = dr[0].ToString();
                        SDList["@ZXBH"] = dr[1].ToString();
                        SDList["@ZXCC"] = dr[2].ToString();
                        SDList["@WLBH"] = dr[3].ToString();
                        SDList["@WLZWMC"] = dr[4].ToString();
                        SDList["@WLEWMC"] = dr[5].ToString();
                        SDList["@WLZXSL"] = dr[7].ToString();
                        SDList["@TXCC"] = dr[8].ToString();
                        SDList["@TXJZ"] = dr[9].ToString();
                        SDList["@TXMZ"] = dr[10].ToString();
                        SDList["@ZXXH"] = dr[11].ToString();
                        bll.AddXDBZXX(SDList);
                    }

                    jsMessage("导入成功！");
                    pageBind();
                    txtPOrderBH.Text = txtEOrderBH.Text;
                    txtPPCNum.Text = txtEPCNum.Text;
                    txtEPCNum.Text = "";
                    #endregion
                }
                else { jsMessage("导入的Excel格式不正确！"); }
            }
            #endregion

            #region 纸箱打印 
            if (strCommandName == "纸箱打印")
            {
                string OrderBH = "";
                string PCNum = "";
                //string ZXBH = "";
                //string ZXCC = "";
                string ZXXH = ""; //同个订单号、批次号下，纸箱序号唯一不重复，用它可以确定唯一一个纸箱，并且纸箱按它排序显示
                foreach (GridViewRow gr in dgList.Rows)
                {
                    if (dgList.DataKeys[gr.RowIndex]["KEYID"].ToString() == strCommandArgument)
                    {
                        OrderBH = dgList.DataKeys[gr.RowIndex]["OrderBH"].ToString();
                        PCNum = dgList.DataKeys[gr.RowIndex]["PCNum"].ToString();
                        //ZXBH = dgList.DataKeys[gr.RowIndex]["ZXBH"].ToString();
                        //ZXCC = dgList.DataKeys[gr.RowIndex]["ZXCC"].ToString();
                        ZXXH = dgList.DataKeys[gr.RowIndex]["ZXXH"].ToString();
                        break;
                    }
                }
                string url = "../REPORTING/ZXXDPRINT.aspx?OrderBH=" + OrderBH + "&PCNum=" + PCNum  + "&ZXXH=" + ZXXH;
                PUB.COMMON.Check.WinOpen(url, 0, 0, 800, 600, "yes", "yes");
            }
            #endregion

            #region 铁箱打印 
            if (strCommandName == "铁箱打印")
            {
                string OrderBH = "";
                string PCNum = "";
                string TXBH = "";
                foreach (GridViewRow gr in dgList.Rows)
                {
                    if (dgList.DataKeys[gr.RowIndex]["KEYID"].ToString() == strCommandArgument)
                    {
                        OrderBH = dgList.DataKeys[gr.RowIndex]["OrderBH"].ToString();
                        PCNum = dgList.DataKeys[gr.RowIndex]["PCNum"].ToString();
                        TXBH = dgList.DataKeys[gr.RowIndex]["TXBH"].ToString();
                        break;
                    }
                }
                string url = "../REPORTING/TXXDPRINT.aspx?OrderBH=" + OrderBH + "&PCNum=" + PCNum + "&TXBH=" + TXBH;
                PUB.COMMON.Check.WinOpen(url, 0, 0, 800, 600, "yes", "yes");
            }
            #endregion

            #region 纸箱批量打印 
            if (strCommandName == "纸箱批量打印")
            {
                string OrderBH = txtPOrderBH.Text ;
                string PCNum = txtPPCNum.Text;
                if (OrderBH == "" || PCNum == "") { jsMessage("请输入订单号、批次号后打印" ); return; }

                string url = "../REPORTING/ZXXDPRINT.aspx?OrderBH=" + OrderBH + "&PCNum=" + PCNum;
                PUB.COMMON.Check.WinOpen(url, 0, 0, 800, 600, "yes", "yes");
            }
            #endregion

            #region 铁箱批量打印 
            if (strCommandName == "铁箱批量打印")
            {
                string OrderBH = txtPOrderBH.Text;
                string PCNum = txtPPCNum.Text;
                if (OrderBH == "" || PCNum == "") { jsMessage("请输入订单号、批次号后打印"); return; }

                string url = "../REPORTING/TXXDPRINT.aspx?OrderBH=" + OrderBH + "&PCNum=" + PCNum;
                PUB.COMMON.Check.WinOpen(url, 0, 0, 800, 600, "yes", "yes");
            }
            #endregion

        }
        #endregion

    }
}